function hopdong()
{
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=hopdong&tmpl=raw";
	var callback = function(){
		if($("#grid_hopdong").length > 0)
		{
			var dataSource = $("#grid_hopdong").data("kendoGrid").dataSource;
			dataSource.read();
			
		}	
	}
	obj.callback = callback;
	return obj;
}

function create_hopdong()
{
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=hopdong&layout=create&tmpl=raw";
	var callback = function(){
		if($("#tab_create_hopdong").length > 0)
		{
			initControl("#tab_create_hopdong");
			initControlhopdong("");
			initCreatehopdong();
		}
	}
	obj.callback = callback;
	return obj;
}

function initControlhopdong(update)
{
	if(update != "update")
	{
		$("#form_create_hopdong input[name='sothangbaohanh']").kendoNumericTextBox({
			format: "n0",
			decimals: 0,
			min:0,
			spinners:false
		});
		
		$("#form_create_hopdong input[name='sodungluongbaohanh']").kendoNumericTextBox({
			format: "n0",
			decimals: 0,
			min:0,
			spinners:false
		});
		
		$("#form_create_hopdong input[name='sotrang']").kendoNumericTextBox({
			format: "n0",
			decimals: 0,
			min:0,
			spinners:false
		});
		
		$("#form_create_hopdong input[name='tienvuotmuc']").kendoNumericTextBox({
			format: "n0",
			decimals: 0,
			min:0,
			spinners:false
		});
		
		$("#form_create_hopdong input[name='hopdong']").kendoNumericTextBox({
			format: "n0",
			decimals: 0,
			min:0,
			spinners:false
		});
		
		$("#form_create_hopdong input[name='thuebao']").kendoNumericTextBox({
			format: "n0",
			decimals: 0,
			min:0,
			spinners:false
		});
	}
	
	$("#serial").chosen({
		max_selected_options:1,
		search_contains: true,
		no_results_text: 'Không tìm thấy:'
	});
	
	$("#serial").change(function(){
		var value = $("#serial option:selected").val();
		if(typeof(value) != "undefined" && $.trim(value) != "")
		{
			var option = $("#serial option:selected");
			var row = option.data("row");
			var mamay = row.ma_machine;
			var tenmay = row.ten_machine;
			$("#mamay").val(mamay);
			$("#tenmay").val(tenmay);
			var sothang = row.sothangbaohanh;
			var sodungluong = row.sodungluongbaohanh;
			$("#form_create_hopdong input[name='sothangbaohanh']").data("kendoNumericTextBox").value(sothang);
			$("#form_create_hopdong input[name='sodungluongbaohanh']").data("kendoNumericTextBox").value(sodungluong);
		}
	})
	
	$("#serial").trigger("change");

	$("#customer").chosentable({
		max_selected_options:1,
		search_contains: true,
		no_results_text: 'Không tìm thấy:',
		template: function (text, templateData) {
	    	return [
	      		"<div class='k-grid-content'><table><tbody><tr><td>" + templateData.ma + "</td>",
	      		"<td>" + templateData.ten + "</td></tr></tbody></table>",      
	    	].join("");
	  	},
	  	headerTemplate:'<div class="k-grid-header"><div class="k-grid-header-wrap"> <table style="width:100%"><thead><tr><th class="k-header" style="width:50%">Mã khách hàng</th><th class="k-header" >Tên khách hàng</th></tr></thead></table></div></div>'
	});
	
	$("#customer").change(function(){
		var value = this.value;
		if(value == "")
		{
			$(this).next().find(".chzn-choices li.search-field").show();
		}else
		{
			$(this).next().find(".chzn-choices li.search-field").hide();
		}
	})
	
	$("#hinhthuc").change(function(){
		var value = this.value;
		$(".hinhthuc_hidden").hide();
		$("." + value).show();
	})
	
	$("#hinhthuc").trigger("change");

}

function edit_hopdong()
{
	var localStorage = window.localStorage;	
	var item_arr = localStorage.getItem("edit_hopdong");
	if(item_arr == null)
	{
		window.location.hash = $hash_url;
		return;
	}
	var item = JSON.parse(item_arr);
	var id = item.id;
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=hopdong&layout=create&tmpl=raw&id="+id;
	var callback = function(){
		if($("#tab_create_hopdong").length > 0)
		{
			initControl("#tab_create_hopdong");
			initControlhopdong("update");
			initCreatehopdong();
		}
	}
	obj.callback = callback;
	return obj;
}

function initCreatehopdong()
{
	var id = 0;
	if($("#form_create_hopdong input[name='id']").length > 0)
	{
		id = $("#form_create_hopdong input[name='id']").val();
		var hinhthuc = $("#form_create_hopdong input[name='hinhthuc']").val();
		if(hinhthuc == "maychothue")
		{
			$(".hinhthuc_hidden").show();
		}
	}
	$("#form_create_hopdong").validate({  
		errorPlacement: function(error, element) {
			if($(element).hasClass("error"))
			{
				hideLoader($('#form_create_hopdong'));
			}
            error.remove();          
        },
        onkeyup:false,
        onfocusout:false,
        rules:{
        	inventory_id:{
                required:true,
                remote:{
        			url:"index.php?option=com_ketnoi&task=hopdong.checkSerial&tmpl=raw&id="+id,
        			data:{
        				serial:function(value){  
        					var val = "";
				        	if($("#form_create_hopdong input[name='id']").length > 0)
				        	{
				        		val = $("#form_create_hopdong input[name='inventory_id']").val();
				        	}else
				        	{
				        		val = $("#form_create_hopdong select[name='inventory_id'] option:selected").val();
				        	}
        	
        					return val;
        				}
        			},
        			beforeSend:function(){
        				showLoader($('#form_create_hopdong'));
        			}
        		}
            },           
            customer_id:{
            	required:true
            }
            
        },
        messages:{
        	inventory_id:{
                required:'Vui lòng chọn serial', 
                remote:'Máy photocopy với số serial này đang hoạt động'
            },    
            customer_id:{
            	required:"Vui lòng chọn khách hàng"
            }
        }
    })
    
    $('#form_create_hopdong').ajaxForm({
    	beforeSubmit:function(){   		
    		showLoader($('#form_create_hopdong'));
    	},
    	success:function(response){
    		var obj = checkJSON(response);
    		if(obj.is_json)
    		{
    			if(!obj.result.success)
        		{
        			var error = obj.result.error;
        			alertify.alert(error);
        		}
    		}
    		else
    		{
    			var container = getMainContent();
    			container.html(response);
    			disableHashchange();   			
    			window.location.hash = "edit_hopdong";
    			initControl("#tab_create_hopdong");
    			initControlhopdong();
    			initCreatehopdong();
    			enableHashchange();
    				
    		}
    		hideLoader($('#form_create_hopdong'));
    	}
    });
}

function edit_hash_hopdong(id)
{
	item = {};
	item.id = id;
	setHash("edit_hopdong",item);
	window.location.hash = "edit_hopdong";
}

function deletehopdong(id)
{
	alertify.confirm("Bạn muốn xóa nhà cung cấp này?", function (e) {
	    if (e) {
	    	showLoader();
			var link = "index.php?option=com_ketnoi&view=hopdong&task=hopdong.delete&tmpl=raw&id="+id;
			$.get(link,"",function(response){
				var dataSource = $("#grid_hopdong").data("kendoGrid").dataSource;
				dataSource.read();
				hideLoader();		
				alertify.set({ delay: 4000 });
				alertify.success("Xóa thành công");
			})
	    } 
	});
}



